package com.demo1112.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/demo1112/demo04.do")
public class Demo04Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如何获取Session
        //首次调用该方法：创建Session会话对象
        //后续每次调用：获取之前创建的Session对象
        HttpSession session = req.getSession();
        //判断该session是不是新创建
        System.out.println(session.isNew());
        //J SESSION ID
        //获取Session的ID值
        System.out.println(session.getId());
        //设置Session5秒后超时
        session.setMaxInactiveInterval(5);
        //马上删除Session
//        session.invalidate();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
